/*
 *  Author: DL
 *  Date: 2021-06-04 15:07:10
 *  Email: duanliang@fengmap.com
 *  LastEditTime: 2021-06-09 14:17:10
 *  LastEditors: DL
 *  LastEditorsEmail: duanliang@fengmap.com
 *  Description:
 *  Copyright  Copyright 2014 - 2021, FengMap, Ltd. All rights reserved.
 */

function updateOrientation(orientationCallback, accelerationIncludingGravityCallback, angleCallback) {
  (function () {
    var angle = 0;

    let timer = setInterval(function () {
      if (orientationCallback) {
        clearInterval(timer);

        // 判断IOS;
        var iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);

        if (!iOS) {
          if (window.DeviceOrientationEvent) {

            window.addEventListener('devicemotion', function (event) {
              accelerationIncludingGravityCallback(event)
            });

            window.addEventListener('deviceorientationabsolute', function (event) {
              angle = event.webkitCompassHeading ? event.webkitCompassHeading : event.alpha;
              if (orientationCallback) {
                orientationCallback(angle);
              }

              if (angleCallback) {
                angleCallback(event);
              }

            });
          }
        } else {
          //通过设备传感器更改定位标注物的方向
          if (!!window.DeviceOrientationEvent) {
            if (typeof DeviceOrientationEvent.requestPermission === 'function') {
              DeviceOrientationEvent.requestPermission()
                .then(function (response) {
                  if (response === 'denied') {
                    console.log('您取消了获取手机方向请求，如再次使用请清除浏览器缓存再试');
                  } else {

                    window.addEventListener('devicemotion', function (event) {
                      accelerationIncludingGravityCallback(event)
                    }, true);


                    window.addEventListener('deviceorientation', function (event) {
                      // iOS设备直接使用webkitCompassHeading
                      if (event.webkitCompassHeading) {
                        // 由于实际是指北的，需要反转角度，下同
                        angle = event.webkitCompassHeading;
                      }

                      if (orientationCallback) {
                        orientationCallback(-angle);
                      }

                      if (angleCallback) {
                        angleCallback(event);
                      }

                    }, true);
                  }
                })
                .catch(function (error) {
                  console.log(error);
                });
            } else {

              window.addEventListener('devicemotion', function (event) {
                accelerationIncludingGravityCallback(event)
              }, true);
              // non iOS 13+
              window.addEventListener('deviceorientation', function (event) {
                if (event.webkitCompassHeading) {
                  angle = event.webkitCompassHeading;
                }

                if (orientationCallback) {
                  orientationCallback(-angle);
                }

                if (angleCallback) {
                  angleCallback(event);
                }

              },
                true,
              );
            }
          }
        }
      } else {
      }
    }, 500);
  })(window);
}

export default updateOrientation;
